package com.mao.action.organization;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.mao.action.BaseAction;
import com.mao.action.jsonvo.organization.JsonVoOrganizationClass;
import com.mao.action.product.ProductClassManageAction;
import com.mao.model.organization.OrganizationClass;
import com.mao.service.organization.IOrganizationClassService;
import com.mao.util.ClassUtil;
import com.mao.util.SessionHelper;

public class OrganizationClassAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private IOrganizationClassService<OrganizationClass> organizationClassService;

	private List<JsonVoOrganizationClass> result;

	private OrganizationClass oc;

	private JsonVoOrganizationClass voOc;

	Logger log = Logger.getLogger(OrganizationClassAction.class);
	
	// 修改分类
	public String jsonUpdateOrganizationClass() {
		OrganizationClass updateOc = organizationClassService.getOrganizationClassById(oc.getId());
		updateOc.setName(oc.getName());
		updateOc.setDescription(oc.getDescription());
		organizationClassService.updateOrganizationClass(updateOc);
		if(oc.getParent().getId() != null){
			OrganizationClass parent = organizationClassService.getOrganizationClassById(oc.getParent().getId());
			if(oc.getParent().getId() == 0){
				updateOc.setParent(null);
			}else{
				updateOc.setParent(parent);
			}
		}else{
			updateOc.setParent(null);
		}
		organizationClassService.updateOrganizationClass(updateOc);
		
		updateOc.setTreePath(ClassUtil.getTreePath(updateOc));
		updateOc.setLevel(ClassUtil.getLevel(updateOc.getTreePath()));
		organizationClassService.updateOrganizationClass(updateOc);
		// 更新所有下级分类的TreePath
		ClassUtil.updateTreePathAndLevel(updateOc, organizationClassService);
		voOc = new JsonVoOrganizationClass();
		voOc.setId(oc.getId());
		voOc.setText(oc.getName());
		
		return SUCCESS;
	}

	// 新增一级分类
	public String jsonSaveOrganizationClass() {
		oc.setParent(null);
		oc.setStore(SessionHelper.getSessionUser().getStore());
		organizationClassService.saveOrganizationClass(oc);
		
		oc.setTreePath(ClassUtil.getTreePath(oc));
		oc.setLevel(ClassUtil.getLevel(oc.getTreePath()));
		organizationClassService.updateOrganizationClass(oc);
		voOc = new JsonVoOrganizationClass();
		voOc.setId(oc.getId());
		voOc.setText(oc.getName());

		return SUCCESS;
	}

	// 新增下级分类
	public String jsonSaveLowOrganizationClass() {
		long parentId = oc.getParent().getId();
		if (parentId != 0) {
			OrganizationClass parent = organizationClassService.getOrganizationClassById(parentId);
			oc.setParent(parent);
		}
		oc.setStore(SessionHelper.getSessionUser().getStore());
		organizationClassService.saveOrganizationClass(oc);

		oc.setTreePath(ClassUtil.getTreePath(oc));
		oc.setLevel(ClassUtil.getLevel(oc.getTreePath()));
		organizationClassService.updateOrganizationClass(oc);
		voOc = new JsonVoOrganizationClass();
		voOc.setId(oc.getId());
		voOc.setText(oc.getName());
		voOc.setParentId(oc.getParent().getId());

		return SUCCESS;
	}

	// 读取往来单位分类
	public String jsonGetAllOrganizationClass() {
		result = new ArrayList<JsonVoOrganizationClass>();
		List<OrganizationClass> ocs = null;
		Map<String,Object> attsMap = null;
		JsonVoOrganizationClass opc = null;
		// 初始页面
		if (oc == null) {
			ocs = organizationClassService.getAllOrganizationClass(SessionHelper.getSessionUser().getStore().getId());
		} else {
			// 读取下级
			oc = organizationClassService.getOrganizationClassById(oc.getId());
			Set<OrganizationClass> ocsets = oc.getLowClass();
			ocs = new ArrayList<OrganizationClass>();
			Iterator<OrganizationClass> it = ocsets.iterator();
			while (it.hasNext()) {
				ocs.add(it.next());
			}
		}
		for (int i = 0; i < ocs.size(); i++) {
			attsMap = new HashMap<String,Object>();
			opc = new JsonVoOrganizationClass();
			oc = ocs.get(i);
			opc.setId(oc.getId());
			opc.setText(oc.getName());
			opc.setState("closed");
			if(oc.getLevel() != 1){
				attsMap.put("parentId", oc.getParent().getId());
				attsMap.put("parentName", oc.getParent().getName());
			}
			attsMap.put("description", oc.getDescription());
			attsMap.put("treepath", oc.getTreePath());
			opc.setAttributes(attsMap);
			result.add(opc);
		}
		return SUCCESS;
	}

	public String deleteOcById() {
		OrganizationClass updateoc = organizationClassService.getOrganizationClassById(oc.getId());
		updateoc.setStatus(OrganizationClass.STATUS_DELETED);
		oc = organizationClassService.updateOrganizationClass(updateoc);
		voOc = new JsonVoOrganizationClass();
		voOc.setId(oc.getId());
		voOc.setText(oc.getName());
		return SUCCESS;
	}

	public String execute() {
		return SUCCESS;
	}

	
	
	public IOrganizationClassService<OrganizationClass> getOrganizationClassService() {
		return organizationClassService;
	}
	
	public void setOrganizationClassService(IOrganizationClassService<OrganizationClass> organizationClassService) {
		this.organizationClassService = organizationClassService;
	}

	public List<JsonVoOrganizationClass> getResult() {
		return result;
	}

	public void setResult(List<JsonVoOrganizationClass> result) {
		this.result = result;
	}

	public OrganizationClass getOc() {
		return oc;
	}

	public void setOc(OrganizationClass oc) {
		this.oc = oc;
	}

	public JsonVoOrganizationClass getVoOc() {
		return voOc;
	}

	public void setVoOc(JsonVoOrganizationClass voOc) {
		this.voOc = voOc;
	}

}
